package xyz.bali16.module.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.starblues.bootstrap.annotation.AutowiredType;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.bali16.application.core.exception.BusinessException;
import xyz.bali16.application.core.utils.AuthUtils;
import xyz.bali16.application.entity.User;
import xyz.bali16.application.service.UserService;
import xyz.bali16.module.article.entity.Article;
import xyz.bali16.module.article.entity.Collect;
import xyz.bali16.module.article.entity.Like;
import xyz.bali16.module.article.mapper.CollectionMapper;
import xyz.bali16.module.article.model.collection.CollectionAddParam;
import xyz.bali16.module.article.model.collection.CollectionPageParam;
import xyz.bali16.module.article.model.like.LikePageParam;
import xyz.bali16.module.article.service.ArticleRankServiceCaller;
import xyz.bali16.module.article.service.ArticleService;
import xyz.bali16.module.article.service.CollectionService;
import xyz.bali16.module.article.service.LikeService;

@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collect> implements CollectionService {
    @Autowired
    @AutowiredType(AutowiredType.Type.PLUGIN_MAIN)
    UserService userService;
    @Autowired
    CollectionService collectionService;
    @Autowired
    ArticleRankServiceCaller articleRankServiceCaller;
    @Autowired
    LikeService likeService;
    @Autowired
    ArticleService articleService;
    @Override
    public IPage<Collect> getPage(CollectionPageParam param) {
        IPage<?> page = new Page<>(param.getCurrent(), param.getSize());
        return this.baseMapper.getCollectionPage(page, param.getTargetId(),param.getUserId());
    }

    @Override
    public void addCollection(CollectionAddParam param) throws Exception {
        Preconditions.checkNotNull(param, "CollectionAddParam 参数不能为为空");
        Collect collect = new Collect();
        BeanUtils.copyProperties(param, collect);
        System.out.println(collect);
        if(isExist(collect.getTargetId())){
            throw new BusinessException("已收藏过");
        }

        save(collect);
        // 处理收藏数量
        LikePageParam likePageParam = new LikePageParam();
        likePageParam.setTargetId(likePageParam.getTargetId());
        likePageParam.setCurrent(0);
        likePageParam.setSize(10);
        CollectionPageParam collectionPageParam = new CollectionPageParam();
        collectionPageParam.setTargetId(param.getTargetId());
        collectionPageParam.setCurrent(0);
        collectionPageParam.setSize(10);
        Integer collectNum = (int) collectionService.getPage(collectionPageParam).getTotal();
        Integer likeNum = (int)likeService.getPage(likePageParam).getTotal();
        articleRankServiceCaller.updateTypeCount("collection",param.getTargetId(),likeNum);
        Article article = articleService.getById(param.getTargetId());
        articleRankServiceCaller.incrTypeCountAndUnionForWeekRank("collection", article.getId(), article.getTitle(), article.getCommentCount(), article.getViews(), article.getGmtCreated(), likeNum, collectNum, true);
    }

    @Override
    public void delete(String likeId) throws Exception {
        if(likeId==null) return;
        User user = userService.getByUsername(AuthUtils.getCurrentUsername());
        Wrapper<Collect> wrapper = Wrappers.<Collect>lambdaQuery().eq(Collect::getTargetId,likeId).eq(Collect::getUserId,user.getUserId());
        remove(wrapper);
        // 处理收藏数量
        LikePageParam likePageParam = new LikePageParam();
        likePageParam.setTargetId(likePageParam.getTargetId());
        likePageParam.setCurrent(0);
        likePageParam.setSize(10);
        CollectionPageParam collectionPageParam = new CollectionPageParam();
        collectionPageParam.setTargetId(likeId);
        collectionPageParam.setCurrent(0);
        collectionPageParam.setSize(10);
        Integer collectNum = (int) collectionService.getPage(collectionPageParam).getTotal();
        Integer likeNum = (int)likeService.getPage(likePageParam).getTotal();
        articleRankServiceCaller.updateTypeCount("like",likeId,likeNum);
        Article article = articleService.getById(likeId);
        articleRankServiceCaller.incrTypeCountAndUnionForWeekRank("collection", article.getId(), article.getTitle(), article.getCommentCount(), article.getViews(), article.getGmtCreated(), likeNum, collectNum, false);
    }

    public boolean isExist(String targetId){
        Wrapper<Collect> wrapper = Wrappers.<Collect>lambdaQuery().eq(Collect::getTargetId,targetId);
        Collect collection = this.getOne(wrapper);
        return collection!=null;
    }
}



